package com.carrental.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carrental.rental.dto.VehicleInspectionDTO;
import com.carrental.rental.entity.VehicleInspection;
import com.carrental.rental.service.VehicleInspectionService;
import com.carrental.rental.vo.VehicleInspectionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 车辆检查记录控制器
 */
@Api(tags = "车辆检查记录管理接口")
@RestController
@RequestMapping("/inspections")
public class VehicleInspectionController {

    @Autowired
    private VehicleInspectionService inspectionService;

    @ApiOperation("创建车辆检查记录")
    @PostMapping
    public Long createInspection(@Valid @RequestBody VehicleInspectionDTO inspectionDTO) {
        return inspectionService.createInspection(inspectionDTO);
    }

    @ApiOperation("根据ID获取车辆检查记录详情")
    @ApiImplicitParam(name = "id", value = "检查记录ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/{id}")
    public VehicleInspectionVO getInspectionById(@PathVariable Long id) {
        return inspectionService.getInspectionById(id);
    }

    @ApiOperation("根据订单ID获取车辆检查记录列表")
    @ApiImplicitParam(name = "orderId", value = "订单ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/order/{orderId}")
    public List<VehicleInspectionVO> getInspectionsByOrderId(@PathVariable Long orderId) {
        return inspectionService.getInspectionsByOrderId(orderId);
    }

    @ApiOperation("根据订单ID和检查类型获取车辆检查记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "检查类型（1-取车检查，2-还车检查）", required = true, dataType = "Integer", paramType = "path")
    })
    @GetMapping("/order/{orderId}/type/{type}")
    public VehicleInspectionVO getInspectionByOrderIdAndType(@PathVariable Long orderId, @PathVariable Integer type) {
        return inspectionService.getInspectionByOrderIdAndType(orderId, type);
    }

    @ApiOperation("分页查询车辆检查记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", defaultValue = "1", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页数量", defaultValue = "10", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "vehicleId", value = "车辆ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "inspectionType", value = "检查类型", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", paramType = "query")
    })
    @GetMapping
    public Page<VehicleInspectionVO> pageInspections(@RequestParam(defaultValue = "1") long current,
                                                     @RequestParam(defaultValue = "10") long size,
                                                     @RequestParam(required = false) String orderNo,
                                                     @RequestParam(required = false) Long vehicleId,
                                                     @RequestParam(required = false) Integer inspectionType,
                                                     @RequestParam(required = false) String startTime,
                                                     @RequestParam(required = false) String endTime) {
        Page<VehicleInspection> page = new Page<>(current, size);
        return inspectionService.pageInspections(page, orderNo, vehicleId, inspectionType, startTime, endTime);
    }

    @ApiOperation("用户确认检查记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "检查记录ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "userSignature", value = "用户签名图片URL", required = true, dataType = "String", paramType = "query")
    })
    @PutMapping("/{id}/confirm")
    public boolean userConfirmInspection(@PathVariable Long id, @RequestParam String userSignature) {
        return inspectionService.userConfirmInspection(id, userSignature);
    }

    @ApiOperation("更新车辆检查记录")
    @PutMapping
    public boolean updateInspection(@Valid @RequestBody VehicleInspectionDTO inspectionDTO) {
        return inspectionService.updateInspection(inspectionDTO);
    }
} 